﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace TalksLog.Tools
{
    public static class Encryptor
    {
        public static string GetMd5String(string source)
        {
            string result = "";

            using (MD5 md5Hash = MD5.Create())
            {
                result = GetHash(md5Hash, source);
            }

            return result;
        }

        public static string GetSH1String(string source)
        {
            string result = "";

            using (SHA1 md5Hash = SHA1.Create())
            {
                result = GetHash(md5Hash, source);
            }

            return result;
        }

        private static string GetHash(HashAlgorithm algorithm, string input)
        {
            byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
    }
}
